<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/11/1
 * Time: 上午11:06
 */

namespace app\index\controller;


use app\common\model\Merchant;
use app\common\model\Mstatis;
use app\common\model\Paylog;
use app\common\model\Statis;
use think\Controller;

class Tstatis extends Controller
{
    public function day() {
        $s = 1;

        $paylog =new Paylog();
        $Statis=new Statis();

        $day = strtotime(date('Y-m-d', strtotime('-1 day')));
        $day2 = strtotime(date('Y-m-d')) - 1;

        $map['add_time'] = array('between', '' . $day . ',' . $day2 . '');
        $map['pay_statu'] = array(array('neq',0),array('neq',8),'and');
        $jorder=$paylog->where($map)->count();
        $mae=$map;
        $mae['tk_price']=array('neq',0.00);
        $torder=$paylog->where($mae)->count();
        $data['orders'] = @($jorder-$torder);
        $jprice=$paylog->where($map)->sum('price');
        $tprice=$paylog->where($map)->sum('tk_price');
        $data['price'] =@($jprice-$tprice);
        $data['tk_price']=$tprice;
        $data['tk_order']=$torder;
        $data['mach'] = $paylog->where($map)->count('distinct(mid)');


        $data['dtime'] = $day;
        $data['addtime'] = date('Y-m-d H:i:s');

        $data['fs'] = 'day';
        $where['dtime'] = $day;


        $where['fs'] = 'day';
        $cx =$Statis->where($where)->count();

//var_dump($data);

        if ($cx > 0) {

            $Statis->save($data,$where);
        } else {
           $Statis->save($data);
        }
    }
    //每日商户统计
    public function machday() {

        $paylog = new Paylog();
        $Merchant=new Merchant();
        $Mstatis=new Mstatis();
        $machlist=$Merchant->field('id,merchant,addtime')->order('id asc')->select();

        foreach($machlist as $k=>$v){
            $day = strtotime(date('Y-m-d', strtotime('-1 day')));
            $day2 = $day+86399;
            $map['mid']=$v['id'];
            $map['add_time'] = array('between', '' . $day . ',' . $day2 . '');
            $map['pay_statu'] = array(array('neq',0),array('neq',8),'and');
            $jorder= $paylog->where($map)->count();
            $mae=$map;
            $mae['tk_price']=array('neq',0.00);
            $torder=$paylog->where($mae)->count();
            $data['orders'] =$jorder-$torder;
            $jprice=$paylog->where($map)->sum('price');
            $tprice=$paylog->where($map)->sum('tk_price');
            $price=@($jprice-$tprice);
            $data['price']=isset($price)?$price:0;
            $data['mach'] = $paylog->where($map)->count('distinct(mid)');
            $data['mid']=$v['id'];
            $data['merchant']=$v['merchant'];
            $data['dtime'] = $day;
            $data['addtime'] = date('Y-m-d H:i:s',$day2);
            $data['machatime']=date('Y-m-d H:i:s',$v['addtime']);
            $data['tk_price']=$tprice;
            $data['tk_order']=$torder;
            $data['fs'] = 'day';
            $where['dtime'] = $day;
            $where['mid']=$v['id'];

            $where['fs'] = 'day';
            $cx = $Mstatis->where($where)->count();



            if ($cx > 0) {

                $add =$Mstatis->allowField(true)->save($data,$where);
            } else {
                $add = $Mstatis->allowField(true)->save($data);
            }}}

    //总交易笔数统计
    public function zmach(){
        $paylog = new Paylog();
        $Merchant=new Merchant();
        $Mstatis=new Mstatis();
        $machlist=$Merchant->field('id,merchant')->order('id asc')->select();
        foreach($machlist as $k=>$v){
            $day =1470758400;
            $day2 =1473523200;
            $map['mid']=$v['id'];
            $map['add_time'] = array('between', '' . $day . ',' . $day2 . '');
            $map['pay_statu'] =array(array('neq',0),array('neq',8),'and');
            $jorder= $paylog->where($map)->count();
            $mae=$map;
            $mae['tk_price']=array('neq',0.00);
            $torder=$paylog->where($mae)->count();
            $data['orders'] =$jorder-$torder;
            $jprice=$paylog->where($map)->sum('price');
            $tprice=$paylog->where($map)->sum('tk_price');
            $price=@($jprice-$tprice);
            $data['price']=isset($price)?$price:0;
            $data['mach'] = $paylog->where($map)->count('distinct(mid)');
            $data['mid']=$v['id'];
            $data['merchant']=$v['merchant'];
            $data['dtime'] = $day;
            $data['addtime'] = date('Y-m-d H:i:s');

            $data['fs'] = 'z';
            $where['dtime'] = $day;
            $where['mid']=$v['id'];

            $where['fs'] = 'z';
            $cx = $Mstatis->where($where)->count();



            if ($cx > 0) {

                $add = $Mstatis->allowField(true)->save($data,$where);
            } else {
                $add = $Mstatis->allowField(true)->save($data);
            }}
    }
    public function week() {
        $paylog = new Paylog();
        $Statis=new Statis();
        $week_start = strtotime('last monday');
        $week_end = strtotime("next sunday");
        $map['add_time'] = array('between', '' . $week_start . ',' . $week_end . '');
        $map['pay_statu'] =array(array('neq',0),array('neq',8),'and');
        $jorder= $paylog->where($map)->count();
        $mae=$map;
        $mae['tk_price']=array('neq',0.00);
        $torder=$paylog->where($mae)->count();
        $data['orders'] =$jorder-$torder;
        $jprice=$paylog->where($map)->sum('price');
        $tprice=$paylog->where($map)->sum('tk_price');

        $data['price'] = @($jprice-$tprice);
        $data['mach'] = $paylog->where($map)->distinct(true)->group('mid')->select();
        // var_dump($data['mach']);
        $data['year'] = date('Y');
        $data['month'] = date('m');
        $data['day'] = date('d');
        $data['fs'] = 'day';
        $where['year'] = date('Y');
        $where['month'] = date('m');
        $where['day'] = date('d');

        $where['fs'] = 'day';
        $cx = $Statis->where($where)->count();
        if ($cx > 0) {

            $add = $Statis->allowField(true)->save($data,$where);
        } else {
            $add = $Statis->allowField(true)->save($data);
        }
    }

    public function month() {
        $paylog = new Paylog();
        $Statis=new Statis();
        $mon_start = strtotime(date("Y-m-01"));
        $BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
        $mon_end = strtotime("$BeginDate +1 month -1 day");
        $map['add_time'] = array('between', '' . $mon_start . ',' . $mon_end . '');
        $map['pay_statu'] =array(array('neq',0),array('neq',8),'and');
        $jorder= $paylog->where($map)->count();
        $mae=$map;
        $mae['tk_price']=array('neq',0.00);
        $torder=$paylog->where($mae)->count();
        $data['orders'] =$jorder-$torder;
        $jprice=$paylog->where($map)->sum('price');
        $tprice=$paylog->where($map)->sum('tk_price');

        $data['price'] = @($jprice-$tprice);
        $data['mach'] = $paylog->where($map)->distinct(true)->field('mid')->count();
        $data['year'] = date('Y');
        $data['month'] = date('m');

        $data['fs'] = 'month';
        $where['year'] = date('Y');
        $where['month'] = date('m');


        $where['fs'] = 'month';
        $cx = $Statis->where($where)->count();
        if ($cx > 0) {

            $add = $Statis->allowField(true)->save($data,$where);
        } else {
            $add = $Statis->allowField(true)->save($data);
        }
    }

    public function year() {
        $paylog=new Paylog();
        $Statis=new Statis();
        $year_start = strtotime(date("Y-01-01"));

        $year_end = strtotime("+1 year", $year_start) - 1;
        $map['add_time'] = array('between', '' . $year_start . ',' . $year_end . '');
        $map['pay_statu'] =array(array('neq',0),array('neq',8),'and');
        $jorder= $paylog->where($map)->count();
        $mae=$map;
        $mae['tk_price']=array('neq',0.00);
        $torder=$paylog->where($mae)->count();
        $data['orders'] =$jorder-$torder;
        $jprice=$paylog->where($map)->sum('price');
        $tprice=$paylog->where($map)->sum('tk_price');

        $data['price'] = @($jprice-$tprice);
        $data['mach'] = $paylog->where($map)->distinct(true)->field('mid')->count();
        $data['year'] = date('Y');


        $data['fs'] = 'year';
        $where['year'] = date('Y');
        $where['fs'] = 'year';
        $cx = $Statis->where($where)->count();
        if ($cx > 0) {

            $add = $Statis->allowField(true)->save($data,$where);
        } else {
            $add =$Statis->allowField(true)->save($data);
        }
    }

    public function dayauto() {
        $s = 1;
        $paylog =new Paylog();
        $Statis=new Statis();
        for ($i = 10; $i > -1; $i--) {
            $day = strtotime(date('Y-m-d', strtotime('-' . $i - $s . ' day')));
            $day2 = strtotime(date('Y-m-d', strtotime('-' . $i . ' day'))) - 1;

            $map['add_time'] = array('between', '' . $day . ',' . $day2 . '');
            $map['pay_statu'] =array(array('neq',0),array('neq',8),'and');
            $jorder= $paylog->where($map)->count();
            $mae=$map;
            $mae['tk_price']=array('neq',0.00);
            $torder=$paylog->where($mae)->count();
            $data['orders'] =$jorder-$torder;
            $jprice=$paylog->where($map)->sum('price');
            $tprice=$paylog->where($map)->sum('tk_price');

            $data['price'] = @($jprice-$tprice);

            $data['dtime'] = $day;
            $data['addtime'] = date('Y-m-d H:i:s', strtotime(date('Y-m-d', strtotime('-' . $i - $s . ' day'))));

            $data['fs'] = 'day';
            $where['dtime'] = $day;


            $where['fs'] = 'day';
            $cx = $Statis->where($where)->count();

//var_dump($data);

            if ($cx > 0) {

                $add = $Statis->allowField(true)->save($data,$where);


            } else {
                $add = $Statis->allowField(true)->save($data);

            }

        }
    }


    public function machdayauto() {

        $s = 1;

        $paylog =new Paylog();
        $Merchant=new Merchant();
        $Mstatis=new Mstatis();
        for ($i = 10; $i > 0; $i--) {

            $machlist=$Merchant->field('id,merchant,addtime')->order('id asc')->select();

            foreach($machlist as $k=>$v){

                $day = strtotime(date('Y-m-d', strtotime('-' . $i - $s . ' day')));
                $day2 = strtotime(date('Y-m-d', strtotime('-' . $i . ' day'))) - 1;
                $map['mid']=$v['id'];
                $map['add_time'] = array('between', '' . $day . ',' . $day2 . '');
                $map['pay_statu'] =array(array('neq',0),array('neq',8),'and');
                $jorder= $paylog->where($map)->count();
                $mae=$map;
                $mae['tk_price']=array('neq',0.00);
                $torder=$paylog->where($mae)->count();
                $data['orders'] =$jorder-$torder;
                $jprice=$paylog->where($map)->sum('price');
                $tprice=$paylog->where($map)->sum('tk_price');
                $data['tk_price']=$tprice;
                $data['tk_order']=$torder;
                $price = @($jprice-$tprice);
                $data['price']=isset($price)?$price:0;
                $data['mach'] = $paylog->where($map)->count('distinct(mid)');
                $data['mid']=$v['id'];
                $data['merchant']=$v['merchant'];
                $data['dtime'] = $day;
                $data['addtime'] = date('Y-m-d',$day2);
                $data['machatime']=date('Y-m-d H:i:s',$v['addtime']);

                $data['fs'] = 'day';
                $where['dtime'] = $day;
                $where['mid']=$v['id'];

                $where['fs'] = 'day';
                $cx =$Mstatis->where($where)->count();



                if ($cx > 0) {

                    $add = $Mstatis->allowField(true)->save($data,$where);
                    echo "2".$Mstatis->getLastSql();
                } else {
                    $add = $Mstatis->allowField(true)->save($data);
                    echo "1".$Mstatis->getLastSql();
                }

            }

        }

    }

}